﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;

using ThousandFunClient.model;
using ThousandFunClient.Util;
using Microsoft.Win32;
using System.Security.Permissions;


namespace ThousandFunClient
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ConfigUtil.initConfig();

            string run = System.Configuration.ConfigurationSettings.AppSettings["runform"];


            if (run.Equals("Fm_TCP"))
            {
                Application.Run(new Fm_TCP());
                return;
            }
            if (run.Equals("Fm_ToolsNet"))
            {
                Application.Run(new Fm_ToolsNet());
                return;
            }
            if (run.Equals("Fm_ToolsNetDB"))
            {
                Application.Run(new Fm_ToolsNetDB());
                return;
            }
            if (run.Equals("Fm_ImageChange"))
            {
                Application.Run(new Fm_ImageChange());
                return;
            }
            if (run.Equals("Fm_XmlImport"))
            {
                Application.Run(new Fm_XmlImport());
                return;
            }
            //Assembly assembly = Assembly.LoadFrom("ThousandFunClient.Fm_ThounsandFunMain");
            //Application.Run((System.Windows.Forms.Form)assembly);
            //Fm_ThounsandFunMain

            //建立注册表
            int count = 0;
            int loginCount=0;
            string loginTime ="";
            string version = "";
            DateTime dt = System.DateTime.Now.AddDays(+45d);
            string timespan = string.Format("{0:yyyyMMdd}",dt);

           
            //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
            RegistryKey mainkey = Registry.LocalMachine;
            RegistryKey subkey = mainkey.OpenSubKey("SOFTWARE\\Register", true);
            string regstr = CryptographyUtil.Encrypt(timespan + ConfigUtil.GetMacAddress());
            
            if (subkey == null)
            {
                subkey = mainkey.CreateSubKey("SOFTWARE\\Register");
                subkey.SetValue("使用次数", count);
                subkey.SetValue("使用时间", regstr);
                subkey.SetValue("使用版本", "0.0.0.0");
            }
            else
            {
                loginCount = (int)subkey.GetValue("使用次数");
                loginTime = (string)subkey.GetValue("使用时间");
                version = (string)subkey.GetValue("使用版本");
            }

            //注册使用时间

            subkey.SetValue("使用次数", loginCount);

            if (!ConfigUtil.valRegStr(loginTime, version))
            {
                MessageBox.Show("您使用软件版权已过期，请注册后再使用！");
                Fm_About fm_About = new Fm_About();
                
                fm_About.Infor = "1";
                Application.Run(fm_About);
                return;
            }
            //{
            //    loginCount = loginCount + 1;
            //    subkey.SetValue("使用次数", loginCount);
            //    if (loginCount > 30)
            //    {
            //        MessageBox.Show("您使用次数已到，请注册后再使用！");

            //    }
            //}
            //else
            

            

            
            subkey.Close();



            
            if (run.Equals("Fm_ThounsandFunMain"))
            {
                Application.Run(new Fm_ThounsandFunMain());
                return;
            }

            if (run.Equals("Fm_MainForm640x480"))
            {
                Application.Run(new Fm_MainForm640x480());
                return;
            }

            
            if (run.Equals("Fm_TCP"))
            {
                Application.Run(new Fm_TCP());
                return;
            }
            
            
        }
    }
}
